
package com.scalar.chiptrack.operations.process.actions;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.process.dao.FabProcessSplitDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.forms.FabProcessSplitForm;
import com.scalar.chiptrack.users.User;

public final class FabProcessSplitAction extends ChipTrackAction {

    public ActionForward execute (ActionMapping mapping, 
                                  ActionForm form, 
                                  HttpServletRequest request, 
                                  HttpServletResponse response) throws Exception 
    {
		String STEP_TYPE = "FAB";
		HttpSession ses = request.getSession();
		HashMap errors = new HashMap();
		
		User user = (User) ses.getAttribute (SessionKeys.USER_KEY);
		if (user == null)
		{
			return (mapping.findForward ("login"));
		}
		
        FabProcessSplitForm fabProcessSplitForm = (FabProcessSplitForm) form;
        
		String [] fabProduct =  fabProcessSplitForm.getFabProduct();
	    String waferStore    = fabProcessSplitForm.getWaferStore();	
		String productId     = (String)ses.getAttribute("productId");
		
		int len = 0;
		for(int i=0; i < fabProduct.length ; i++ )
		{
			if( fabProduct[i] != null && fabProduct[i].trim().length() > 0 )
			{
				len++;
			}
		}
		String [] tempFabProduct = new String[len];
		int x = 0 ; 
		for(int i=0; i < fabProduct.length ; i++ )
		{
			if( fabProduct[i] != null && fabProduct[i].trim().length() > 0 )
			{
				tempFabProduct[x] = fabProduct[i];
				x++; 
			}
		}
		
		if( !isTokenValid(request) )
		{	
			System.out.println(" Into reset token ");
			FabProcessSplitDAO fabProcessSplitDAO = new FabProcessSplitDAO();
			FabProcessSplitForm fabProcessSplitForm1 = null;
			fabProcessSplitForm1 = fabProcessSplitDAO.getFabTrackId(productId);
			fabProduct = fabProcessSplitForm1.getFabProduct();
			String [] fabTrackId = fabProcessSplitForm1.getFabTrackId();
			
			ArrayList fabTrackList = new ArrayList();

			for(int i=0;i<fabTrackId.length;i++)
			{
				fabTrackList.add(fabTrackId[i]);		
			}
			request.setAttribute("fabProduct",tempFabProduct);
			request.setAttribute("fabtrackList",fabTrackList);
				           
			return (mapping.findForward("success"));
		}
		String dupNames = ProductDao.checkDupInProductMapTable( fabProduct , -1 , STEP_TYPE );
		if( dupNames.length() < 1 )
		{
			System.out.println(" Into Main Block ");
			ArrayList fabTrackList = new ArrayList();
			FabProcessSplitDAO fabProcessSplitDao  = new FabProcessSplitDAO();

			fabTrackList = fabProcessSplitDao.save( fabProcessSplitForm.getFabProduct() , fabProcessSplitForm.getWaferStore() , productId );           
			resetToken(request);

			
			request.setAttribute( "fabProduct" , tempFabProduct );
			request.setAttribute( "fabtrackList" , fabTrackList );
			saveToken(request); 
	    	return (mapping.findForward("success"));
		} 
		else
		{
			errors.put ( "FAB name(s) already exists " , dupNames );
			request.setAttribute ( SessionKeys.ERROR_MESSAGES , errors );
			request.setAttribute( "fabprocess" , fabProcessSplitForm );
			return (mapping.findForward("failure"));
		}
	}
	
}